<template>
  <div style="padding:20px;" id="app">
    <div class="panel panel-primary">
      <div class="panel-heading">自定义table组件绑定json</div>
      <table class="table table-bordered table-striped text-center">
        <thead>
        <tr>
          <th>ID</th>
          <th>用户名</th>
          <th>年龄</th>
          <th>所在学院</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(user,index) in users">
          <td>{{ index + 1 }}</td>
          <td>{{ user.name }}</td>
          <td>{{ user.age }}</td>
          <td>{{ user.school }}</td>
          <td>
            <button v-on:click="remove(index)">remove</button>
          </td>
        </tr>
        <tr>
          <td></td>
          <td><input type="text" id="name" v-model="user.name"/></td>
          <td><input type="text" id="age" v-model="user.age"/></td>
          <td><input type="text" id="school" v-model="user.school"/></td>
          <td>
            <button @click="insert">insert</button>
          </td>
        </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'hello',
    data () {
      return {
        user: {'name': '', 'age': '', 'school': ''},
        users: [
          {'name': '小弟1号', 'age': '3', 'school': '物理信息学院'},
          {'name': '小弟2号', 'age': '5', 'school': '数学与数据科学学院'},
          {'name': '小弟3号', 'age': '4', 'school': '计算机学院'}
        ]
      }
    },
    methods: {
      insert: function () {
        this.users.push(this.user)
      },
      remove: function (index) {
        this.users.splice(index, 1)
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  h1, h2 {
    font-weight: normal;
  }

  ul {
    list-style-type: none;
    padding: 0;
  }

  li {
    display: inline-block;
    margin: 0 10px;
  }

  a {
    color: #42b983;
  }
  tr,th{
    text-align:center;
  }
</style>
